package com.jyy.prefabricated.controller;

import com.jyy.prefabricated.common.AjaxResult;
import com.jyy.prefabricated.entity.AboutUs;
import com.jyy.prefabricated.service.AboutUsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 关于我们
 * @version 1.0.0
 * @author: zjj
 */
@Slf4j
@RequestMapping("/api")
@RestController
public class AboutUsController {

    @Autowired
    private AboutUsService aboutUsService;

    /**
     * 新增关于我们
     * @param aboutUs
     * @return
     */
    @PostMapping("/about-us")
    public AjaxResult insert(@Valid @RequestBody AboutUs aboutUs){
        log.info("Rest request to insert aboutUs:{}",aboutUs);
        int id = aboutUsService.insert(aboutUs);
        AboutUs result = aboutUsService.selectById(id);
        return AjaxResult.success(result);
    }

    /**
     * 更新关于我们
     * @param aboutUs
     * @return
     */
    @PutMapping("/about-us")
    public AjaxResult update(@Valid @RequestBody AboutUs aboutUs){
        log.info("Rest request to update aboutUs:{}",aboutUs);
        aboutUsService.update(aboutUs);
        AboutUs result = aboutUsService.selectById(aboutUs.getId());
        return AjaxResult.success(result);
    }

    /**
     * 删除关于我们
     * @param id
     * @return
     */
    @DeleteMapping("/about-us/{id}")
    public AjaxResult delete(@PathVariable Integer id){
        log.info("Rest request to delete aboutUs by id:{}",id);
        int row = aboutUsService.delete(id);
        if(row > 0){
            return AjaxResult.success();
        } else {
            return AjaxResult.error();
        }
    }

    /**
     * 查询关于我们
     * @return
     */
    @GetMapping("/about-us")
    public AjaxResult select(){
        log.info("Rest request to find aboutUs");
        AboutUs result = aboutUsService.selectOne();
        return AjaxResult.success(result);
    }
}
